package com.njupt.wuaiagent.rag;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @Author: wujiaming
 * @CreateTime: 2025/5/12 19:40
 * @Description: 恋爱大使向量数据库的配置（初始化基于内存的向量数据库 Bean）
 * @Version: 1.0
 */


@Configuration
public class LoveAppVectorStoreConfig {

    //RAG知识库文档加载器
    @Resource
    private LoveAppDocumentLoader loveAppDocumentLoader;

    //手动切片
    @Resource
    MyTokenTextSplitter myTokenTextSplitter;


    //文档关键词提取
    @Resource
    MyKeyWordEnricher myKeyWordEnricher;

    /**
     * 基于内存的向量存储器，构建自定义的向量存储器
     * @param dashscopeEmbeddingModel
     * @return
     */
    @Bean
    VectorStore loveVectorStore(EmbeddingModel dashscopeEmbeddingModel){
        SimpleVectorStore simpleVectorStore = SimpleVectorStore.builder(dashscopeEmbeddingModel).build();
        
        //DocumentReader 加载文档
        List<Document> documents = loveAppDocumentLoader.loadMarkDown();
        //DocumentTransformer 使用TokenTextSplitterToken自主切分文档
//        List<Document> splitDocument = myTokenTextSplitter.splitCustomized(documents);

        //DocumentTransformer 使用KeywordMetadataEnricher实现关键词的提取
        List<Document> enricherDocument = myKeyWordEnricher.enrichDocument(documents);

        //DocumentWriter，写入向量数据库
        simpleVectorStore.add(enricherDocument);
        return simpleVectorStore;



    }
}
